<?php namespace Home\Controller;

use Hdphp\Controller\Controller;
use \Common\Model\City;
use \Common\Model\Company;
use \Common\Model\Job;

//筛选控制器
class ListController extends Controller{
    protected $cityModel;
    public function __init(){
        $this->cityModel=new City();
    }

	//职位筛选页面
	public function index(){
		//获得职位和公司信息
    	$companyModel=new Company();
		$jobModel=new Job();
    	$kw=Q('get.k');
    	if(!$kw) $kw='_';
    	$keyword=mb_substr($kw,0,3,'utf-8');
        $keyword=addslashes($keyword);
        //筛选
        //地址栏效果
        $param=isset($_GET['param'])? explode('_', Q('get.param')) : array_fill(0,9,'0');
        //组合where条件
        $sqlArr='';
        $order='';
        if($param[0]!=0){
            if ($param[1]==0) {
                $cidList=$this->cityModel->where("city_pid={$param[0]}")->lists('city_id');
                $cidList=implode(',',$cidList);
                $sqlArr .= "lg_job.lg_city_city_id in ({$cidList}) AND ";
            }else{
                $sqlArr .= "lg_job.lg_city_city_id = {$param[1]} AND ";
            }
        }
        if($param[2]!='0') $sqlArr .= "experience = '{$param[2]}' AND ";
        if($param[3]!='0') $sqlArr .= "education = '{$param[3]}' AND ";
        if($param[4]!='0') $sqlArr .= "financing = '{$param[4]}' AND ";
        if($param[5]!='0') $sqlArr .= "industry = '{$param[5]}' AND ";
        if($param[8]!='0') $sqlArr .= "jobtype = '{$param[8]}' AND ";
        if($param[7]!='0'){
            switch ($param[7]) {
                case '2k以下':
                    $sqlArr .= "lowmoney <= 2 AND";
                    break;
                case '50k以上':
                    $sqlArr .= "lowmoney >= 50 AND";
                    break;
                default:
                    $moneyArr=explode('-',str_replace('k','',$param[7]));
                    $sqlArr .= "lowmoney BETWEEN {$moneyArr[0]} AND {$moneyArr[1]} AND ";
                    break;
            }
        }
        if($param[6]!='0') $order = "order by jobsendtime desc ";

        //分页
        $num=$jobModel->join('company','gsid','=','lg_company_gsid')->where("{$sqlArr} online=1 and (jname like '%{$keyword}%' or gsname like '%{$keyword}%' or jobaddress like '%{$keyword}%') ")->count();
        $Page=Page::row(8)->make($num);
        $data=Db::select("select * from lg_company join lg_job on lg_company.gsid=lg_job.lg_company_gsid where {$sqlArr} online=1 and ( jname like '%{$keyword}%' or gsname like '%{$keyword}%' or jobaddress like '%{$keyword}%' ) {$order} limit " . Page::limit());

    	foreach ($data as $k => $v) {
    		//获得所属城市
			$area=$this->cityModel->where("city_id={$data[$k]['lg_city_city_id']}")->find();
			$city=$this->cityModel->where("city_id={$area['city_pid']}")->pluck('city_name');
			$data[$k]['city']=$city;
			$data[$k]['area']=$area['city_name'];
    	}
        //统计数量
        $num=$num>500?'500+':$num;
        //选中的城市
        $showCity=$this->cityModel->chooseCity(9,2);
        //热门城市
        $cityData=$this->cityModel->hotCity();
        //其他城市
        $otherCity=$this->cityModel->otherCity();
        View::with('cityData',$cityData);
        View::with('otherCity',$otherCity);
        View::with('showCity',$showCity);
        View::with('num',$num);
        View::with('param',$param);
        View::with('Page',$Page);
    	View::with('data',$data);
		View::make();
	}

	//公司筛选页面
	public function company(){
        $jobModel=new Job();
		$companyModel=new Company();
		//获得公司信息
		$kw=Q('get.k');
    	if(!$kw) $kw='_';
    	$keyword=mb_substr($kw,0,2,'utf-8');
        $keyword=addslashes($keyword);
        //筛选
        //地址栏效果
        $param=isset($_GET['param'])? explode('_', Q('get.param')) : array_fill(0,5,'0');
        //组合where条件
        $sqlArr='';
        $order='';
        if($param[0]!=0) $sqlArr .= "lg_city_city_id = {$param[0]} AND ";
        if($param[1]!='0') $sqlArr .= "financing = '{$param[1]}' AND ";
        if($param[2]!='0') $sqlArr .= "industry = '{$param[2]}' AND ";
        if($param[3]!='0') $order .= "order by jobnumber desc ";

        //分页
        $num=$companyModel->where("{$sqlArr} logo!='' and shortname like '%{$keyword}%'")->count();
        $Page=Page::row(4)->make($num);
        $data=Db::select("select * from lg_company where {$sqlArr} logo != '' and shortname like '%{$keyword}%' {$order} limit " . Page::limit());

		foreach ($data as $k => $v) {
			//获得所属城市
			$city=$this->cityModel->where("city_id={$data[$k]['lg_city_city_id']}")->pluck('city_name');
			$data[$k]['city']=$city;
			//获得在招职位个数
			$data[$k]['jobNum']=$jobModel->where("lg_company_gsid={$data[$k]['gsid']} AND online=1")->count();
		}
        //统计数量
        $num=$num>500?'500+':$num;
        //选中的城市
        $showCity=$this->cityModel->chooseCity(5);
        //热门城市
        $cityData=$this->cityModel->hotCity();
        //其他城市
        $otherCity=$this->cityModel->otherCity();
        View::with('cityData',$cityData);
        View::with('otherCity',$otherCity);
        View::with('showCity',$showCity);
        View::with('num',$num);
        View::with('param',$param);
        View::with('Page',$Page);
		View::with('data',$data);
		View::make();
	}

}